package com.gpframework.module.controller.manufacture;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.manufacture.domain.GpOutputRate;
import com.gpframework.module.manufacture.domain.req.GpOutputRateAdd;
import com.gpframework.module.manufacture.domain.req.GpOutputRateReq;
import com.gpframework.module.manufacture.domain.req.GpOutputRateUpdate;
import com.gpframework.module.manufacture.domain.vo.GpOutputRateVo;
import com.gpframework.module.manufacture.service.IGpOutputRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName GpOutputRateController
 * @Author gpframework
 * @Date 2023-11-10
 * @Version 1.0.0
 * @Description 出码率基础Controller
 */
@Validated
@Api(tags = "出码率基础")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/manufacture/outputRate" )
public class GpOutputRateController extends BaseController {

    private final IGpOutputRateService iGpOutputRateService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-11-10
  * @Param gpOutputRate
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:list')")
    @GetMapping("/list")
    public TableDataInfo<GpOutputRateVo> list(GpOutputRateReq gpOutputRateReq)
    {
        startPage();
        QueryWrapper<GpOutputRate> lqw = new QueryWrapper<GpOutputRate>();
        gpOutputRateReq.generatorQuery(lqw,true);
        List<GpOutputRateVo> list = iGpOutputRateService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpOutputRate
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:add')" )
    @Log(title = "添加出码率基础" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpOutputRateAdd gpOutputRateAdd) {
        GpOutputRate gpOutputRate = Convert.convert(new TypeReference<GpOutputRate>() {}, gpOutputRateAdd);

        return toAjax(iGpOutputRateService.save(gpOutputRate) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpOutputRate
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:edit')" )
    @Log(title = "修改出码率基础" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GpOutputRateUpdate gpOutputRateUpdate) {
        GpOutputRate gpOutputRate = Convert.convert(new TypeReference<GpOutputRate>() {}, gpOutputRateUpdate);
        return toAjax(iGpOutputRateService.updateById(gpOutputRate) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpOutputRate
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:export')" )
    @Log(title = "导出出码率基础" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpOutputRateReq gpOutputRateReq) {
        QueryWrapper<GpOutputRate> lqw = new QueryWrapper<GpOutputRate>();
        gpOutputRateReq.generatorQuery(lqw,true);
        List<GpOutputRateVo> list = iGpOutputRateService.listVo(lqw);
        ExcelUtil<GpOutputRateVo> util = new ExcelUtil<GpOutputRateVo>(GpOutputRateVo. class);
        return util.exportExcel(list, "outputRate" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpOutputRate
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpOutputRateVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpOutputRateService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-11-10
     * @Param gpOutputRate
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:remove')" )
    @Log(title = "删除出码率基础" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpOutputRateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("下载模板")
    @PreAuthorize("@ss.hasPermi('order:outputRate:export')" )
    @Log(title = "下载模板" , businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate" )
    public AjaxResult importTemplate() {
        ExcelUtil<GpOutputRateVo> util = new ExcelUtil<GpOutputRateVo>(GpOutputRateVo.class);
        return util.importTemplateExcel("出码率基础表");
    }

    /**
     * @Description 导入
     * @Author gpframework
     * @Date 2023-06-16
     * @Param gpProductShelves
     * @Return TableDataInfo
     */
    @ApiOperation("导入")
    @PreAuthorize("@ss.hasPermi('manufacture:outputRate:add')" )
    @Log(title = "导入" , businessType = BusinessType.INSERT)
    @PostMapping("/import")
    public AjaxResult importList(MultipartFile file) throws Exception {
        if (file == null) {
            return AjaxResult.error("获取不到导入文件");
        }
        boolean excel = ExcelUtil.isExcel(file.getOriginalFilename());
        if (!excel) {
            return AjaxResult.error("导入文件格式错误，请导入excel文件");
        }
        // 删除旧数据
        iGpOutputRateService.remove(new QueryWrapper<GpOutputRate>().eq("del_status", YesNoEnum.NO.getKey()));
        // 导入新数据
        ExcelUtil<GpOutputRateVo> util = new ExcelUtil<GpOutputRateVo>(GpOutputRateVo.class);
        List<GpOutputRateVo> excelList = util.importCacheExcel(file.getInputStream());
        List<GpOutputRate> list = Convert.convert(new TypeReference<List<GpOutputRate>>() {}, excelList);
        return iGpOutputRateService.saveBatch(list) ? AjaxResult.success("导入成功") : AjaxResult.error();
    }
}
